/*
 * Seat.java
 *
 * Created on October 4, 2007, 5:47 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package drb;

import java.sql.*;

/**
 *
 * @author Asger
 */
public class Seat implements Comparable<Seat> {
    
    public int row;
    public char position;
    public int wagonNumber;
    public boolean firstClass;
    public boolean isBed = false;
    
    /** Creates a new instance of Seat */
    public Seat() {
    }
    
    public int compareTo(Seat o) {
        if (wagonNumber != o.wagonNumber)
            return wagonNumber - o.wagonNumber;
        if (row != o.row)
            return row - o.row;
        return (int)(position - o.position);
    }
    
    public String toString()
    {
        if (isBed)
            return "Bed " + row + position + ", wagon " + wagonNumber;
        
        return "Seat " + row + position + ", wagon " + wagonNumber;
    }
    
    public void fetchDetails(Connection con) throws SQLException {
        String sql = "SELECT wagon_type FROM wagons WHERE wagon_number="+wagonNumber;
        ResultSet result = con.createStatement().executeQuery(sql);
        if (!result.next())
            return;
        isBed = result.getString("wagon_type").equals("B");
    }
    
    public static Seat parse(ResultSet result, String prefix) throws SQLException {
        Seat seat = new Seat();
        seat.row = result.getInt(prefix + "seat_row");
        seat.position = result.getString(prefix + "seat_position").charAt(0);
        seat.wagonNumber = result.getInt(prefix + "wagon_number");
        seat.firstClass = result.getString(prefix + "class").equals("A");
        return seat;
    }

    public int hashCode() {
        return row*123 + (int)position*12463 + wagonNumber*241;
    }
    
    public boolean equals(Object o) {
        if (o == null)
            return false;
        if (getClass() != o.getClass())
            return false;
        Seat seat = (Seat)o;
        return seat.row == row && seat.position == position && seat.wagonNumber == wagonNumber;
    }
}




